package com.ruoyi.quartz.task;


import com.alibaba.fastjson.JSONObject;
import com.ruoyi.bizsys.domain.ExtMonnifyOrder;
import com.ruoyi.bizsys.domain.RepaymentOrder;
import com.ruoyi.bizsys.mapper.ExtMonnifyOrderMapper;
import com.ruoyi.bizsys.mapper.RepaymentOrderMapper;
import com.ruoyi.bizsys.service.IRepaymentOrderService;
import com.ruoyi.business.component.RepaymentMqSendService;
import com.ruoyi.business.service.IDoRepaymentLastService;
import jodd.util.Base64;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 处理解冻钱包，传入sql
 *
 * @author ruoyi
 */
@Slf4j
@Component("dealWalletBackTask")
public class DealWalletBackTask {

    @Autowired
    IRepaymentOrderService repaymentOrderService;
    @Autowired
    RepaymentOrderMapper repaymentOrderMapper;
    @Autowired
    IDoRepaymentLastService doRepaymentLastService;


    /**
     * 需要 Base64decode后，在这个网站 https://base64.us/
     * sqlStr 参入进行执行
     */
    public void dealBySqlStr(String sqlStr) {
        sqlStr = Base64.decodeToString(sqlStr);
        log.info("处理解冻钱包 问题bug的，手动触发 Start>>>>>>>>>>,{}", sqlStr);
        List<RepaymentOrder> list = repaymentOrderMapper.dealBySqlStr(sqlStr);
        if (!CollectionUtils.isEmpty(list)) {
            log.info("处理解冻钱包 问题bug的::开始处理：size:{}", list.size());
            for (int i = 0; i < list.size(); i++) {
                dealRepaymentOrder(list.get(i));
            }
        }
    }

    private void dealRepaymentOrder(RepaymentOrder repaymentOrder) {
        log.info("处理解冻钱包 问题bug的::开始处理：repaymentOrder:{}", repaymentOrder.getRepaymentNo());
        repaymentOrderService.updateRepaymentOrderAndOffsetFaild(repaymentOrder);
        boolean isDeffer = false;
        if (repaymentOrder.getActionType().indexOf("Defer") != -1) {
            isDeffer = true;
        }
        doRepaymentLastService.updateWalletOrderToFailed(repaymentOrder.getPayTransSerialNo(), isDeffer);
    }
}